//
//  Rchat.m
//  HandheldHospital
//
//  Created by 张鹏的电脑 on 16/3/3.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "RchatConteroller.h"
#import "AppDelegate.h"
#import "ZC_AsynchronousBlock.h"
#import <AVUser.h>
#import "DataManager.h"

#define kShared [DataManager defaultManager]

@interface RchatConteroller ()<RCIMUserInfoDataSource>


@end

@implementation RchatConteroller

-(instancetype)init{
    self = [super init];
    if (self ) {
        //添加聊天功能(单聊,群聊,聊天室...)
        [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),@(ConversationType_GROUP),@(ConversationType_DISCUSSION),@(ConversationType_GROUP)]] ;
        //添加群组
        //[self setCollectionConversationTypeArray:@[@(ConversationType_PRIVATE)]];
        
    }
    
    return self;
    
}

//点击会话列表中Cell的回调
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
         conversationModel:(RCConversationModel *)model
               atIndexPath:(NSIndexPath *)indexPath{
    if (conversationModelType == RC_CONVERSATION_MODEL_TYPE_COLLECTION) {
        RchatConteroller *cc = [[RchatConteroller alloc] init];
        
        NSArray *array = [NSArray arrayWithObject:[NSNumber numberWithInt:model.conversationType]];
        [cc setDisplayConversationTypes:array];
        [cc setCollectionConversationType:nil];
        
        cc.isEnteredToCollectionViewController = YES;
        
        [self.navigationController pushViewController:cc animated:YES];
        
    }else if (model.conversationType == ConversationType_PRIVATE){
        
        RCConversationViewController *vc = [[RCConversationViewController alloc] init];
        vc.conversationType = model.conversationType;
        vc.targetId = model.targetId;
        
        vc.title = model.conversationTitle;
        [self.navigationController pushViewController:vc animated:YES];
    }else if((model.conversationType = ConversationType_GROUP) ){
        RCConversationViewController *gg = [[RCConversationViewController alloc] init];
        gg.targetId = @"qq";
        gg.conversationType = model.conversationType;
        
        gg.title = @"群聊天";
        [self.navigationController pushViewController:gg animated:YES];
        
        
    }
    
}

//修改 cell 的 title 颜色
- (void)willDisplayConversationTableCell:(RCConversationBaseCell *)cell
                             atIndexPath:(NSIndexPath *)indexPath{
    RCConversationModel *model = self.conversationListDataSource[indexPath.row];
    
    if (model.conversationType == ConversationType_PRIVATE) {
        RCConversationCell *conver = (RCConversationCell *)cell;
        conver.conversationTitle.textColor = [UIColor redColor];
       
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;

   
 
    [[RCIM sharedRCIM] connectWithToken:kShared.token success:^(NSString *userId) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[RCIM sharedRCIM] setUserInfoDataSource:self];
            
        });
        
    } error:^(RCConnectErrorCode status) {
        
    } tokenIncorrect:^{
        
    }];
    

    
    
}


//实现协议方法
- (void)getUserInfoWithUserId:(NSString *)userId
                   completion:(void (^)(RCUserInfo *userInfo))completion{
    if ([userId isEqualToString:@"1121017372@qq.com"]) {

        RCUserInfo *userIf = [[RCUserInfo alloc] init];
        userIf.userId = userId;
        userIf.name  = [AVUser currentUser].username;
        userIf.portraitUri = @"http://www.51ps.com/upfile/2007/11/200711234317140356694.jpg";
        return completion(userIf);

    }else if([userId isEqualToString:@"1121017372@qq.com"]){
        RCUserInfo *userInfo = [[RCUserInfo alloc] init];
        userInfo.userId = userId;
        userInfo.name = @"张鹏";
        userInfo.portraitUri = @"http://pic.nipic.com/2007-11-08/2007118192311804_2.jpg";

        return completion(userInfo);


    }else if([userId isEqualToString:@"1121017372@qq.com"]){
        RCUserInfo *userInfo = [[RCUserInfo alloc] init];
        userInfo.userId = userId;

        userInfo.name = @"章超";
        userInfo.portraitUri = @"http://pic.nipic.com/2007-12-16/20071216105113620_2.jpg";
        return completion(userInfo);

    }

    return completion(nil);

}




@end
